const fs = require('fs')
const path = require('path')
const commentSQL = require('../db/commentSQL')
const userSQL=require('../db/userSQL')
const {db,pool}=require('../db/db')

const getList = async (goodsId, c, p) => {
  let result = await db(commentSQL.queryAll, [])
  let user = await db(userSQL.queryAll, [])
  let data=result.filter(item => item.goods_id === goodsId)
  let data1=JSON.stringify(data)
  let data2=JSON.parse(data1)
  let str=''
  data2.forEach(item=>{
    let userId=item.user_id
    let username=user.find(item=>item.id===userId).username
    let val=JSON.stringify(item)
    str+=val.slice(0,-1)+',"username":"'+username+'"},'
  })
  let info1='['+str.slice(0,-1)+']'
  let info=JSON.parse(info1)
  return info
}

const remove = async (commentId) => {
  console.log(commentId)
  await pool.query(commentSQL.deleteComment,[commentId],(err,result)=>{undefined
    if(err) throw err;
    console.log(result);
  });
  return 1;
}

module.exports = {
  getList,
  remove
}
